const config = require('./config')

const { StargateClient } = require('@cosmjs/stargate');

const Logger = require('./logger');

const rpc = process.argv[2];
const num = process.argv[3];
const logfile = process.argv[4];
console.log('rpc, num, logfile', rpc, num, logfile);
const logger = new Logger("press", logfile, logfile, "info");

function idleSleep (time) {
    return new Promise((resolve) => setTimeout(resolve, time));
}


const main= async ()=>{
    
        while(1){
            let oldTime = new Date().getTime();
            logger.info("oldTime: ", oldTime);
              try {  
            const client = await StargateClient.connect(rpc);
            let run_time = new Date().getTime();
            let height = await client.getHeight();
            logger.info("run time: ",run_time, ' height: ', height);
            let i = 0;
            while(i<num) {
                logger.info("run time: ",run_time, ' -- i ======= ', i);
                //let chainID = await client.getChainId();
                //logger.info("With client, chain id:",  chainID);
                //let balance = await client.getAllBalances("noble194k8nn7cnq3gtwe7ap3mzd0at3432zlxt43c0l");// <-- replace with your generated address
                //logger.info("With client: balance: ", balance);
                let block = await client.getBlock(height-i)
                logger.info("with client: get block: ",block.id);
                i++;
            }

            await idleSleep(3000)

        }
        catch (e) {
            logger.error("e: ", e);
            let newTime = new Date().getTime();
            logger.info("newTime ", newTime);
            let spendTime = newTime - oldTime;

            logger.info('spendTime: ', spendTime);
            logger.info('spendTime / 1000: ', spendTime/ 1000)

    }


    }


}
main();
